package com.itheima.service.impl;


import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.CustomException;
import com.itheima.common.TokenHolder;
import com.itheima.dao.*;
import com.itheima.dto.OrdersPageDto;
import com.itheima.pojo.*;
import com.itheima.service.OrdersService;
import com.itheima.vo.DetailVo;
import com.itheima.vo.OrdersDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author Administrator
 */
@Service
public class OrdersServiceImpl implements OrdersService {



    @Resource
    private OrdersMapper orderMapper;

    @Resource
    private OrdersDetailMapper ordersDetailMapper;

    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Resource
    private AddressBookMapper addressBookMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public Page<Orders> getPage(OrdersPageDto dto) {

        String currentId = TokenHolder.getCurrentId();

        Integer skip = (dto.getPage()-1)*dto.getPageSize();

        Map<Object, Object> map = new HashMap<>();
        map.put("skip",skip);
        map.put("limit",dto.getPageSize());
        map.put("beginTime",dto.getBeginTime());
        map.put("endTime",dto.getEndTime());
        map.put("number",dto.getNumber());

        List<Orders> page = orderMapper.getPage(map);
        Integer total = orderMapper.selectAll();
        Page<Orders> ordersPage = new Page<Orders>();

        ordersPage.setTotal(total);
        ordersPage.setRecords(page);
        ordersPage.setCurrent(dto.getPage());
        ordersPage.setSize(dto.getPageSize());

        return ordersPage;

    }

    /**
     * 修改订单状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public Boolean modifyStatus(String id, Integer status) {


        int i = orderMapper.updateOrderStatus(id, status);

        return i>0;
    }

    /**
     * 订单列表
     *
     * @param page 当前页
     * @param pageSize 数量
     * @return
     */
    @Override
    public Page<OrdersDetailVo> getDetail(Integer page, Integer pageSize) {

       String userId = TokenHolder.getCurrentId();
        Integer skip = (page-1)*pageSize;
        List<OrdersDetailVo> ordersDetail = orderMapper.getOrdersDetail(skip, pageSize,userId);
        Integer total = orderMapper.selectAll();

        for (OrdersDetailVo ordersDetailVo : ordersDetail) {
            List<DetailVo> detailList = ordersDetailMapper.getNameAndNumber(ordersDetailVo.getId());
            ordersDetailVo.setOrderDetails(detailList);
        }

        Page<OrdersDetailVo> pageList = new Page<>();
        pageList.setCurrent(page);
        pageList.setSize(pageSize);
        pageList.setRecords(ordersDetail);
        pageList.setTotal(total);

        return pageList;
    }

    /**
     * 提交订单
     *
     * @param orders
     * @return
     */
    @Override
    public Boolean submitOrders(Orders orders) {
        String userId = TokenHolder.getCurrentId();
        User user = userMapper.selectUser(userId);

        String address = addressBookMapper.selectDetailById(orders.getAddressBookId());
        if (address==null) {
            throw new CustomException("地址为空");
        }

        orders.setId(new Snowflake().nextIdStr());
        orders.setNumber(orders.getId());
        orders.setUserId(userId);
        orders.setUserName(user.getName());
        orders.setPhone(user.getPhone());
        orders.setAmount(orderMapper.getAmount(userId));
        //BigDecimal amount = orders.getAmount();
        orders.setAddress(address);

        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(userId);
        List<OrderDetail> list = new ArrayList<>();

        for (ShoppingCart shoppingCart : shoppingCarts) {
            BigDecimal amount1 = shoppingCart.getAmount();
            Integer number = shoppingCart.getNumber();
           // amount.add(amount1.multiply(new BigDecimal(number)));
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCart, orderDetail);
            orderDetail.setId(new Snowflake().nextIdStr());
            orderDetail.setOrderId(orders.getId());
            list.add(orderDetail);
        }
       // orders.setAmount(amount);
        int submit = orderMapper.submit(orders);
        int insert = ordersDetailMapper.insert(list);
        shoppingCartMapper.deleteAll(userId);

        return submit>0;
    }
}
